//
//  KDThirdNotyController.m
//  WangJingApp
//
//  Created by 魏强 on 17/2/8.
//  Copyright © 2017年 KDTech. All rights reserved.
//

#import "KDThirdNotyController.h"
#import "KDThirdPostCell.h"
#import "KDUnitPerson.h"
#import "KDUnit.h"
#import "WQSeparatorView.h"
#import "KDFourthNotyController.h"
#import "UIViewController+BackButtonHandler.h"

@interface KDThirdNotyController ()<UITableViewDelegate, UITableViewDataSource, KDThirdPostCellDelegate, KDFourthNotyControllerDelegate, BackButtonHandlerProtocol>
/**  <#Description#> */
@property (nonatomic, strong) NSMutableArray *infoArr;
@property (nonatomic, copy) NSString * cellId;
/**  全选按钮 */
@property (nonatomic, weak)  UIButton *allSelectBtn ;
/**  <#Description#> */
@property (nonatomic, strong) UIView *header;
/**  最顶部的lb */
@property (nonatomic, weak) UILabel *titleLabel;
/**  <#Description#> */
@property (nonatomic, weak) UITableView *tableView;
/**  确认按钮 */
@property (nonatomic, weak) UIButton *confirmBtn;

/**  所有被选中的模型集合 */
@property (nonatomic, strong) NSMutableArray *selectedArr;

@end

@implementation KDThirdNotyController
#pragma mark - 懒加载区
- (NSMutableArray *)infoArr
{
    if (!_infoArr) {
        _infoArr = [NSMutableArray array] ;
    }
    return _infoArr;
}


#pragma mark - 生命周期区
- (void)viewDidLoad {
    [super viewDidLoad];
    self.notyNum = @"0";
    self.view.backgroundColor = BackGroundColor;
    [self setupTitleLabel];
    [self setupTableView];
    [self setupConfirmBtn];
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;@"FakeUnitData"
}

#pragma mark - 板块布置区
- (void)setupTitleLabel{
    UILabel *titleLabel = [[UILabel alloc]init];
    titleLabel.frame = CGRectMake(15, 0, screenW - 15, 44);
    self.titleLabel = titleLabel;
    titleLabel.text = @"广东省公安厅省厅网警总队(0)";
//    titleLabel.layer.borderColor = BackGroundColor.CGColor;
//    titleLabel.layer.borderWidth = 0.5;
    titleLabel.textColor = WQColor(168, 168, 168);
    titleLabel.backgroundColor = WQColor(242, 243, 245);
    titleLabel.textAlignment = NSTextAlignmentLeft;
    [self.view addSubview: titleLabel];
}

- (void)setupTableView{
    UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0  , CGRectGetMaxY(self.titleLabel.frame), screenW,  self.view.height *0.6) style:UITableViewStylePlain];
    self.tableView = tableView;
    [self.view addSubview:self.tableView];
    tableView.delegate = self;
    tableView.dataSource = self;
    self.tableView.tableFooterView = [UIView new];
    self.tableView.separatorColor = BackGroundColor;
    self.tableView.backgroundColor = BackGroundColor;
    //    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 64, 0);
    self.cellId = @"KDThirdPostCell";
    UINib *xib = [UINib nibWithNibName:self.cellId bundle:nil];
    
    [self.tableView registerNib:xib forCellReuseIdentifier:self.cellId];
    //    self.tableView.separatorColor = WQColor(245, 245, 245);
    
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadData)];
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    [footer setTitle:@"" forState:MJRefreshStateIdle];
    // 设置footer
    self.tableView.footer = footer;
    [self.tableView.header beginRefreshing];
}

- (void)setupConfirmBtn{
    UIButton *confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    confirmBtn.frame = CGRectMake(10, CGRectGetMaxY(self.tableView.frame) + 20, screenW - 20, 44);
    [confirmBtn setTitle:@"确定" forState:UIControlStateNormal];
    [confirmBtn addTarget:self action:@selector(confirmBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    confirmBtn.backgroundColor = KDTintColor;
    [confirmBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];;
    self.confirmBtn = confirmBtn;
    [self.view addSubview:confirmBtn];
}
#pragma mark - 事件点击区
/**
 *  全选按钮被点击了
 */
- (void)allSelectBtnClick:(UIButton *)btn{
    self.allSelectBtn.selected = !self.allSelectBtn.selected;
    
    for (KDUnit *unit in self.infoArr) {
        unit.select = btn.selected;
        unit.allSelect = btn.selected;
//        KDLog(@"%d", unit.select);
    }
    
    [self.tableView reloadData];
    [self refreashSelectArr];
}
/**
 *  确定按钮被点击了
 */
- (void)confirmBtnClick:(UIButton *)confirmBtn{
    [self.navigationController popViewControllerAnimated:YES];
    if (_delegate && [_delegate respondsToSelector:@selector(thirdNotyControllerBackToSecondWithInfoArr:)] ) {
        [_delegate thirdNotyControllerBackToSecondWithInfoArr:self.selectedArr];
    }
}
#pragma mark - 网络请求区
- (NSMutableArray *)getFakeData
{
    
    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"FakeUnitData" ofType:@"plist"];
    
    //    NSArray *data = [[NSArray alloc] initWithContentsOfFile:plistPath];
    return [KDUnit objectArrayWithFile:plistPath];
    
}
- (void)loadData{
    self.infoArr = [self getFakeData];
    [self.tableView reloadData];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.tableView.header endRefreshing];
    });
}
- (void)loadMoreData{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.tableView.footer endRefreshing];
    });
}
#pragma mark - 通知接收区



#pragma mark - Table view data source


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.infoArr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    KDThirdPostCell *cell = [KDThirdPostCell cellWithTableView:tableView];
    cell.model = self.infoArr[indexPath.row];
    cell.delegate = self;
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (!self.header) {
        UIView *header = [[UIView alloc]initWithFrame:CGRectMake(0, 0, screenW, 54)];
        self.header = header;
        
        WQSeparatorView *view = [[WQSeparatorView alloc]init];
        [self.header addSubview:view];
        view.separatorMode = WQViewSeparatorModeTopAndBottom;
        
        view.separatorColor = WQColor(238, 238, 238);
        view.backgroundColor = [UIColor whiteColor];
        view.frame = CGRectMake(0, 0, screenW, 44);
        UIButton *allSelectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        allSelectBtn.frame = CGRectMake(15, 0, 65, 44);
        [allSelectBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 10, 0, 0)];
        [allSelectBtn setTitle:@"全选" forState:UIControlStateNormal];
        allSelectBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        [allSelectBtn setImage:[UIImage imageNamed:@"select"] forState:UIControlStateNormal];
        [allSelectBtn setImage:[UIImage imageNamed:@"select_sl"] forState:UIControlStateSelected];
        [allSelectBtn addTarget:self action:@selector(allSelectBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        //    <#buttonName#>.backgroundColor = [UIColor whiteColor];
        [allSelectBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];;
        self.allSelectBtn = allSelectBtn;
        [view addSubview:allSelectBtn];
    }
    
    return self.header;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 54;
}



- (void)refreashSelectArr{
    self.selectedArr = [NSMutableArray array];
    for (KDUnit *model  in self.infoArr) {
        if (model.select) {
            [self.selectedArr addObject:model];
        }
    }
    _titleLabel.text = [NSString stringWithFormat:@"广东省公安厅省厅网警总队(%ld)", self.selectedArr.count];

}


#pragma mark - KDThirdPostCellDelegate

- (void)thirdPostCell:(KDThirdPostCell *)cell selectBtnClick:(UIButton *)selectBtn{
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    KDUnit *model = self.infoArr[indexPath.row];
    model.select = selectBtn.selected;
    
    model.allSelect = selectBtn.selected;
    [self.infoArr replaceObjectAtIndex:indexPath.row withObject:model];
    [self refreashSelectArr];
}
/**
 *  右边箭头按钮被点击了
 */
- (void)thirdPostCell:(KDThirdPostCell *)cell rightBtnClick:(UIButton *)rightBtn{
    KDFourthNotyController *four = [[KDFourthNotyController alloc]init];
    four.delegate = self;
    four.model = cell.model;
    [self.navigationController pushViewController:four animated:YES];
}
#pragma mark - KDFourthNotyControllerDelegate
- (void)fourthNotyControllerBackToThird{
    [self.tableView reloadData];
}

#pragma mark - BackButtonHandlerProtocol
- (BOOL)navigationShouldPopOnBackButton{
    if (_delegate && [_delegate respondsToSelector:@selector(thirdNotyControllerBackToSecondWithInfoArr:)] ) {
        [_delegate thirdNotyControllerBackToSecondWithInfoArr:self.selectedArr];
    }
    return YES;
}

@end
